package com.bosssoft.cloud.wechat.common.controller;

import com.bosssoft.cloud.wechat.infrastructure.util.VerifyCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author liqw
 * @description 基础服务*
 * @create 2018-09-07 10:18
 **/
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/base")
@Api(value = "BaseController", description = "基础服务相关api")
public class BaseController {

    /**
     * TODO 重写
     */
    @ApiOperation(value = "获取网站图片验证码", notes = "返回byte数据")
    @GetMapping("/queryVerifyImg")
    public void verifyImg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        //生成随机字串
        String verifyCode = VerifyCodeUtil.generateVerifyCode(4);
        log.debug("生成图片验证码为: {}", verifyCode);
        //存入会话session
        HttpSession session = request.getSession(true);
        VerifyCodeUtil.setValidationCode(session, verifyCode.toLowerCase());
        //生成图片
        int w = 200, h = 80;
        VerifyCodeUtil.outputImage(w, h, response.getOutputStream(), verifyCode);
    }
}
